﻿// event handling variables
var ctrlDown = false;
var shiftDown = false;

document.onclick =  hideMenu;
document.onkeydown = eventKeyDown;
document.onkeyup = eventKeyUp;
document.onkeypress = eventKeyPress;

var colorArray = new Array("#3399ff","#00cc66","#ff3300","#cf43d9");

// global function to create draggability on a given element
function addDrag (element) {
	var newDrag = new Draggable(element, {revert:true});
	addDragObservers(element);
    return element;
}

// global function to add observers to drag elements
function addDragObservers (element) {
    Event.observe(element, 'mousedown', activeEditor.cachedEventSelectCell);
    Event.observe(element, 'contextmenu', activeEditor.cachedShowMenu);
    if ( (activeEditorPageId != 'liveControl') && (activeEditorPageId != 'multiPackage') ) {
        Event.observe(element, 'dblclick', activeEditor.cachedDrillDown);
    }
        
    if (activeEditor.systemLevel > 2) return;
    
    Event.observe(element, 'mousemove', activeEditor.cachedCalculateDrop);
    Event.observe(element, 'mouseup', activeEditor.cachedMouseUp); 
}

// global function to remove observers from drag elements
function removeDragObservers (element) {
    Event.stopObserving(element, 'mousedown', activeEditor.cachedEventSelectCell);
    Event.stopObserving(element, 'contextmenu', activeEditor.cachedShowMenu);
    Event.stopObserving(element, 'dblclick', activeEditor.cachedDrillDown);
        
    if (activeEditor.systemLevel > 2) return;
    
    Event.stopObserving(element, 'mousemove', activeEditor.cachedCalculateDrop);
    Event.stopObserving(element, 'mouseup', activeEditor.cachedMouseUp); 
}

// global function to create droppability on a given element
function addDrop (element) {
	var acceptableCSS = ['dragCell','selectedDragCell']
	var hover = (activeEditor.systemLevel < 3) ? '' : 'dropHover';
	Droppables.add(element, {
        accept: acceptableCSS,
        greedy: true,
        hoverclass: hover,
        onDrop: function(dragEl, dropEl) {
            //if (dropEl.firstDescendant()) return;
            if (!activeEditor.validateDrop(dragEl, dropEl)) return;
			
			// clear highlight for drag drop editors
			if (activeEditor.systemLevel < 3) activeEditor.clearDropHighlight();
			
			// create temp drag element
        	var tmpDrag = document.createElement('div');
            tmpDrag.id = dragEl.id;
            tmpDrag.innerHTML = dragEl.innerHTML;
            tmpDrag.className = dragEl.className;
            tmpDrag.style.backgroundColor = dragEl.style.backgroundColor;
            tmpDrag.style.color = dragEl.style.color;
            
            if (activeEditor.systemLevel < 3) {
                tmpDrag.style.width = activeEditor.cellWidth + "px";
			    tmpDrag.style.padding = "0 " + activeEditor.cellPadding + "px";
			    tmpDrag.style.height = activeEditor.cellHeight + "px";
			} else {
                tmpDrag.style.width = dragEl.style.width;
			    tmpDrag.style.height = dragEl.style.height;
			}
            
            //remove old drag element
            removeDragObservers(dragEl);
            dragEl.remove();
            
			$('activeDrag').innerHTML = "active drag: " + Draggables.activeDraggable.element.id;
            Draggables.activeDraggable.destroy();
            
			dropEl.appendChild(tmpDrag);
			addDrag(tmpDrag);
			
			if (activeEditor.systemLevel < 3) activeEditor.clearSelectedCells();
			    
			activeEditor.selectCell(tmpDrag);
			
			setPageModified(true);
			
			if ((activeEditor.systemLevel == 3) && (dropEl.id == 'comboEditorDefaultAsset')) {
			    //var color = "#" + dragEl.style.backgroundColor;
			    $('comboEditor_Color').style.backgroundColor = dragEl.style.backgroundColor
			}
        }
    });
    return element;
}

function addDropObservers(element) {
	Event.observe(element, 'mousedown', activeEditor.cachedEventSelectCell);
	Event.observe(element, 'contextmenu', activeEditor.cachedShowMenu);
 
}

function removeDropObservers(element) {
	Event.stopObserving(element, 'mousedown', activeEditor.cachedEventSelectCell);
	Event.stopObserving(element, 'contextmenu', activeEditor.cachedShowMenu);
 
}

// function to hide context sensitive menu
function hideMenu (e) {
    if ( (activeEditorPageId != 'liveControl') && (activeEditorPageId != 'multiPackage') ) {
        $('multiSelectMenu').style.display = "none";
        $('scheduleMenu').style.display = "none";
        $('packageListMenu').style.display = "none";
        $('packageMenu').style.display = "none";
        $('eventMenu').style.display = "none";
        $('comboMenu').style.display = "none";
        $('assetMenu').style.display = "none";
        $('assetMenuShort').style.display = "none";
        $('hotKeyMenu').style.display = "none";
        $('scheduleEditor_Messages').style.display = "none";
    } else {
        $('multiSelectMenu').style.display = "none";
        $('packageListMenu').style.display = "none";
        $('eventMenu').style.display = "none";
        $('hotKeyMenu').style.display = "none";
    }
}

// event handler for onkeydown
function eventKeyDown (e) {	
	if (e.which == 17) ctrlDown = true;
	if (e.which == 16) shiftDown = true;
}
	
// event handler for onkeyup
function eventKeyUp (e) {
    ctrlDown = false;
	shiftDown = false;
}	

// event handler for onkeypress
function eventKeyPress (e) {
    //  handle enter key
	if (e.keyCode == 13) handleEnterKeyPress();
	
	var selectedId;
    if (!activeEditor) return;
    if (activeEditor.selectedRegion.length > 0) {
        selectedId = activeEditor.selectedRegion.first().id
    } else if (activeEditor.selectedCell) {
        selectedId  = activeEditor.selectedCell.id;
    }
	var selectedOffsetX;
	var selectedOffsetY;
	if (selectedId) {
	    if (activeEditor.systemLevel == 0) {
		    selectedOffsetX = parseInt(selectedId.substring(selectedId.indexOf('ddList_') + 7, selectedId.indexOf('_drop')),10);
		    selectedOffsetY = parseInt(selectedId.substr(selectedId.indexOf('drop_') + 5),10);
		    // keyboard navigation
	        if (e.keyCode == 38) {
			    if (selectedOffsetY > 0) {
				    activeEditor.clearSelectedCells();
				    selectedOffsetY = selectedOffsetY - 1;
				    var newSelectedId = activeEditor.tagId + "_ddList_" + selectedOffsetX + "_drop_" + selectedOffsetY;
				    activeEditor.selectCell($(newSelectedId));
			    }
	        }
	        if (e.keyCode == 39) {
			    if (selectedOffsetX < activeEditor.numLists-1) {
				    activeEditor.clearSelectedCells();
				    selectedOffsetX = selectedOffsetX + 1;
				    var newSelectedId = activeEditor.tagId + "_ddList_" + selectedOffsetX + "_drop_" + selectedOffsetY;
				    activeEditor.selectCell($(newSelectedId));
			    }
	        }
	        if (e.keyCode == 40) {
			    if (selectedOffsetY < activeEditor.numIntervals-1) {
				    activeEditor.clearSelectedCells();
				    selectedOffsetY = selectedOffsetY + 1;
				    var newSelectedId = activeEditor.tagId + "_ddList_" + selectedOffsetX + "_drop_" + selectedOffsetY;
				    activeEditor.selectCell($(newSelectedId));
			    }
	        }
	        if (e.keyCode == 37) {
			    if (selectedOffsetX > 0) {
				    activeEditor.clearSelectedCells();
				    selectedOffsetX = selectedOffsetX - 1;
				    var newSelectedId = activeEditor.tagId + "_ddList_" + selectedOffsetX + "_drop_" + selectedOffsetY;
				    activeEditor.selectCell($(newSelectedId));
			    }		
		    }
		} else {  // timeline list (up and down only)
		    var selected = activeEditor.selectedCell;
			if (e.keyCode == 38) {  // up
			    if (activeEditor.selectedRegion.length > 0) {
		            selected = activeEditor.selectedRegion.first();
		        }
			    if (selected.previousSiblings().length > 0) {
				    activeEditor.clearSelectedCells();
				    activeEditor.selectCell(selected.previous());
			    }
	        }
	        if (e.keyCode == 40) {  // down
                if (activeEditor.selectedRegion.length > 0) {
		            selected = activeEditor.selectedRegion.last();
		        }
			    if (selected.nextSiblings().length > 0) {
				    activeEditor.clearSelectedCells();
				    activeEditor.selectCell(selected.next());
			    }
	        }	        
		}
	}

	// handle cut, copy, paste
	if (activeEditor.selectedCell || activeEditor.selectedRegion || activeEditor.selectedList) {
	    if (ctrlDown) {
	        if (e.which == 99) activeEditor.copyToClip();  // c button
	        if (e.which == 120) activeEditor.cutToClip();  // x button
	        if (e.which == 118) activeEditor.checkPasteFromClip();  // v button
	        //if (e.which == 97) activerEditor.addFromIcon();  // a button
	        //if (e.which == 114) activeEditor.remove();  // r button
	    }	
	}
	
	// handle hot keys
    if (e.which == 49) {  // 1 keypress
    	activeEditor.addFromHK(1);
    }
    if (e.which == 50) {  // 2 keypress
    	activeEditor.addFromHK(2);
    }	
    if (e.which == 51) {  // 3 keypress
    	activeEditor.addFromHK(3);
    }
    if (e.which == 52) {  // 4 keypress
    	activeEditor.addFromHK(4);
    }		
    if (e.which == 53) {  // 5 keypress
    	activeEditor.addFromHK(5);
    }	    
}

function handleEnterKeyPress() {
    if (activeEditorPageId.search(/List/) > -1 && activeEditorPageId != 'scheduleList') {
        prepareSearch();
    } else if (activeEditorPageId.search(/Editor/) > -1 || activeEditorPageId == 'multiPackage') {
        getSearchResults();
    } else if (activeEditorPageId == 'liveControl') {
        // do nothing
        return;
    } else {
        // do nothing again
        return;
    }
}


